document.getElementById('scrapeAndReplaceBtn').addEventListener('click', async function() {
    const url = document.getElementById('targetResumeUrl').value;
    const statusDiv = document.getElementById('scraperStatus');
    
    if (!url) {
        statusDiv.innerHTML = '<p style="color:red;">请输入有效的简历URL</p>';
        return;
    }
    
    try {
        statusDiv.innerHTML = '<p><i class="fas fa-spinner fa-spin"></i> 正在获取简历信息...</p>';
        
        const response = await fetch(`/api/scrape-resume?url=${encodeURIComponent(url)}`);
        const { success, data, message } = await response.json();
        
        if (!success) throw new Error(message || '获取简历失败');
        
        replaceResumeContent(data);
        statusDiv.innerHTML = '<p style="color:green;"><i class="fas fa-check"></i> 简历信息已更新!</p>';
        
    } catch (error) {
        statusDiv.innerHTML = `<p style="color:red;"><i class="fas fa-times"></i> 错误: ${error.message}</p>`;
    }
});

function replaceResumeContent(data) {
    // 更新个人信息
    document.querySelector('.profile-card h1').textContent = data.basicInfo.name;
    document.querySelector('.job-title').textContent = data.basicInfo.jobTitle;
    document.querySelector('.profile-img').src = data.basicInfo.profileImg;

    // 更新联系方式
    const contactItems = document.querySelectorAll('.contact-info li');
    contactItems[0].innerHTML = `<i class="fas fa-phone"></i> ${data.contacts.phone}`;
    contactItems[1].innerHTML = `<i class="fas fa-envelope"></i> ${data.contacts.email}`;
    contactItems[3].innerHTML = `<i class="fab fa-github"></i> ${data.contacts.github}`;

    // 更新教育背景
    const eduItem = document.querySelector('.timeline-item');
    eduItem.querySelector('.timeline-date').textContent = data.education.period;
    eduItem.querySelector('h3').textContent = data.education.school;
    eduItem.querySelector('.subtitle').textContent = `${data.education.major} | 大专`;
    eduItem.querySelector('p:nth-of-type(2)').textContent = `主修课程: ${data.education.courses}`;

    // 更新技能
    const skillItems = [
        'Java/Spring Boot', 
        'MySQL/Oracle', 
        'HTML/CSS/JS', 
        'Python/Django', 
        'Linux/Git'
    ];
    skillItems.forEach((skill, i) => {
        document.querySelectorAll('.skill-item p')[i].textContent = skill;
    });

    // 更新项目经验
    const projectsContainer = document.querySelector('.projects');
    projectsContainer.innerHTML = '';
    
    data.projects.forEach(proj => {
        const projectCard = document.createElement('div');
        projectCard.className = 'project-card';
        projectCard.innerHTML = `
            <div class="project-header">
                <h3>${proj.name}</h3>
                <span class="project-date">${proj.period}</span>
            </div>
            <div class="project-tech">
                ${proj.technologies.split(',').map(tech => `<span>${tech.trim()}</span>`).join('')}
            </div>
            <div class="project-details">
                <p>${proj.description}</p>
                <ul>
                    <li>${proj.responsibilities}</li>
                </ul>
            </div>
        `;
        projectsContainer.appendChild(projectCard);
    });

    // 更新获奖情况
    const awardsContainer = document.querySelector('.awards');
    awardsContainer.innerHTML = '';
    
    data.awards.forEach(award => {
        const awardItem = document.createElement('div');
        awardItem.className = 'award-item';
        awardItem.innerHTML = `
            <div class="award-icon">
                <i class="fas fa-medal gold"></i>
            </div>
            <div class="award-content">
                <h3>${award}</h3>
            </div>
        `;
        awardsContainer.appendChild(awardItem);
    });

    // 更新自我评价
    document.querySelector('.self-assessment').innerHTML = `
        <p>${data.selfAssessment}</p>
    `;

    // 更新页脚
    document.querySelector('.footer p').innerHTML = `© ${new Date().getFullYear()} ${data.basicInfo.name} - 个人简历`;
    document.getElementById('last-update').textContent = new Date().toLocaleDateString();
}